package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.BrandConfig;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.BranConfigService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import com.tbit.uqbike.webmanager.dao.core.BrandConfigDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author chenmengran
 * @Description: 品牌配置信息
 * @date 2019\12\7 000716:41
 */
@Service
public class BranConfigServiceImpl implements BranConfigService {

    @Autowired
    private BrandConfigDao brandConfigDao;
    @Autowired
    private AccountService accountService;
    @Autowired
    private PurviewService purviewService;

    @Override
    public BrandConfig getByUrl(String url) {
        return brandConfigDao.selectByUrl(url);
    }

    @Override
    public void insert(BrandConfig brandConfig, LoginInfo loginInfo) {
        if (!loginInfo.type.equals(UserConstant.USER_ADMIN)
                && !loginInfo.type.equals(UserConstant.USER_BRAND)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }

        checkExist(brandConfig);

        brandConfigDao.insert(brandConfig);
    }

    private void checkExist(BrandConfig brandConfig) {
        BrandConfig byHost = brandConfigDao.getByHost(brandConfig.getBrandHost());
        if (Objects.nonNull(byHost) && !byHost.getAccountId().equals(brandConfig.getAccountId())) {
            throw new BaseException("brand.config.hostName.exist");
        }
        BrandConfig byName = brandConfigDao.getByName(brandConfig.getBrandName());
        if (Objects.nonNull(byName) && !byName.getAccountId().equals(brandConfig.getAccountId())) {
            throw new BaseException("brand.config.brandName.exist");
        }
    }

    @Override
    public void editBrandConfig(BrandConfig brandConfig, LoginInfo loginInfo) {
        Account account = accountService.getAccountById(loginInfo, brandConfig.getAccountId());
        if (account == null || !account.getType().equals(UserConstant.USER_ADMIN)) {
            throw new BaseException(SystemCodeEnum.NOT_EXIST);
        }
        checkExist(brandConfig);
        BrandConfig bg = brandConfigDao.getByAccountId(brandConfig.getAccountId());
        if (bg == null) {
            brandConfigDao.insert(brandConfig);
        } else {
            brandConfigDao.update(brandConfig);
        }
    }

    @Override
    public BrandConfig getByAccountId(LoginInfo loginInfo, Integer accountId) {
        if (!purviewService.purviewCheck(loginInfo.id, accountId)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        return brandConfigDao.getByAccountId(accountId);
    }

    @Override
    public BrandConfig getByHost(String brandHost) {
        return brandConfigDao.getByHost(brandHost);
    }

    @Override
    public List<BrandConfig> getByAccountIds(List<Integer> accountIds) {
        return CollUtil.isNotEmpty(accountIds) ? brandConfigDao.getByAccountIds(accountIds) : Collections.emptyList();
    }

    @Override
    public BrandConfig getByName(String brandName) {
        return brandConfigDao.getByName(brandName);
    }

}
